<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="nickname">
      <el-input v-model="dataForm.nickname" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="电话号码" prop="telephone">
      <el-input v-model="dataForm.telephone" placeholder="电话号码"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-input v-model="dataForm.sex" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="headImgurl">
      <el-input v-model="dataForm.headImgurl" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="注册时间" prop="registerDate">
      <el-input v-model="dataForm.registerDate" placeholder="注册时间"></el-input>
    </el-form-item>
    <el-form-item label="省" prop="provinceId">
      <el-input v-model="dataForm.provinceId" placeholder="省"></el-input>
    </el-form-item>
    <el-form-item label="市" prop="cityId">
      <el-input v-model="dataForm.cityId" placeholder="市"></el-input>
    </el-form-item>
    <el-form-item label="县" prop="countyId">
      <el-input v-model="dataForm.countyId" placeholder="县"></el-input>
    </el-form-item>
    <el-form-item label="详细地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="详细地址"></el-input>
    </el-form-item>
    <el-form-item label="公司" prop="company">
      <el-input v-model="dataForm.company" placeholder="公司"></el-input>
    </el-form-item>
    <el-form-item label="身份证正面" prop="idcardFront">
      <el-input v-model="dataForm.idcardFront" placeholder="身份证正面"></el-input>
    </el-form-item>
    <el-form-item label="身份证反面" prop="idcardReverse">
      <el-input v-model="dataForm.idcardReverse" placeholder="身份证反面"></el-input>
    </el-form-item>
    <el-form-item label="自我介绍" prop="introduction">
      <el-input v-model="dataForm.introduction" placeholder="自我介绍"></el-input>
    </el-form-item>
    <el-form-item label="身份：0、管理员，1、经纪人，2、普通用户" prop="status">
      <el-input v-model="dataForm.status" placeholder="身份：0、管理员，1、经纪人，2、普通用户"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          nickname: '',
          telephone: '',
          email: '',
          sex: '',
          password: '',
          headImgurl: '',
          registerDate: '',
          provinceId: '',
          cityId: '',
          countyId: '',
          address: '',
          company: '',
          idcardFront: '',
          idcardReverse: '',
          introduction: '',
          status: ''
        },
        dataRule: {
          nickname: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          telephone: [
            { required: true, message: '电话号码不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          headImgurl: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          registerDate: [
            { required: true, message: '注册时间不能为空', trigger: 'blur' }
          ],
          provinceId: [
            { required: true, message: '省不能为空', trigger: 'blur' }
          ],
          cityId: [
            { required: true, message: '市不能为空', trigger: 'blur' }
          ],
          countyId: [
            { required: true, message: '县不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '详细地址不能为空', trigger: 'blur' }
          ],
          company: [
            { required: true, message: '公司不能为空', trigger: 'blur' }
          ],
          idcardFront: [
            { required: true, message: '身份证正面不能为空', trigger: 'blur' }
          ],
          idcardReverse: [
            { required: true, message: '身份证反面不能为空', trigger: 'blur' }
          ],
          introduction: [
            { required: true, message: '自我介绍不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '身份：0、管理员，1、经纪人，2、普通用户不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.nickname = data.user.nickname
                this.dataForm.telephone = data.user.telephone
                this.dataForm.email = data.user.email
                this.dataForm.sex = data.user.sex
                this.dataForm.password = data.user.password
                this.dataForm.headImgurl = data.user.headImgurl
                this.dataForm.registerDate = data.user.registerDate
                this.dataForm.provinceId = data.user.provinceId
                this.dataForm.cityId = data.user.cityId
                this.dataForm.countyId = data.user.countyId
                this.dataForm.address = data.user.address
                this.dataForm.company = data.user.company
                this.dataForm.idcardFront = data.user.idcardFront
                this.dataForm.idcardReverse = data.user.idcardReverse
                this.dataForm.introduction = data.user.introduction
                this.dataForm.status = data.user.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'nickname': this.dataForm.nickname,
                'telephone': this.dataForm.telephone,
                'email': this.dataForm.email,
                'sex': this.dataForm.sex,
                'password': this.dataForm.password,
                'headImgurl': this.dataForm.headImgurl,
                'registerDate': this.dataForm.registerDate,
                'provinceId': this.dataForm.provinceId,
                'cityId': this.dataForm.cityId,
                'countyId': this.dataForm.countyId,
                'address': this.dataForm.address,
                'company': this.dataForm.company,
                'idcardFront': this.dataForm.idcardFront,
                'idcardReverse': this.dataForm.idcardReverse,
                'introduction': this.dataForm.introduction,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
